package com.dgz.csmall.product.config;

import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;


/**
 * @Author duguozheng
 * @Date 2023/05/10  9:56
 * @Description Knife4j配置类
 * @Version 1.0
 */
@Slf4j
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfiguration {

    /*【重要】指定Controller包路径 */
    private String basePackage = "com.dgz.csmall.product.controller";

    /* 分组名称 */
    private String groupName = "product";

    /* 主机名 */
    private String host = "https://java.dgz.com";

    /* 标题 */
    private String title = "酷鲨商城在线API文档--管理员管理";

    /* 简介 */
    private String description = "酷鲨商城在线API文档--管理员管理";

    /* 服务条款URL */
    private String termsOfServiceUrl = "https://www.apache.org/licenses/LICENSE-2.0";

    /* 联系人 */
    private String contactName = "Java教学研发部";

    /* 联系网址 */
    private String contactUrl = "https://java.dgz.cn";

    /* 联系邮箱 */
    private String contactEmail = "dgz0000@yeah.net";

    /* 版本号 */
    private String version = "1.0.0";

    @Autowired
    private OpenApiExtensionResolver openApiExtensionResolver;

    public Knife4jConfiguration() {
        log.debug("创建配置类对象：Knife4jConfiguration");
    }

    /**
     * 在使用Swagger构建API文档时用于生成Swagger的Docket 对象，该对象定义了API文档的基本信息。
     * 方法调用了 Docket 类的一系列链式方法来设置不同的选项：
     * @return 一个 Docket 对象，用于构建 Swagger API 文档。
     */
    @Bean
    public Docket docket() {
        String groupName = "1.0.0";
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .host(host)
                .apiInfo(apiInfo())
                .groupName(groupName)
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any())
                .build()
                .extensions(openApiExtensionResolver.buildExtensions(groupName));
        return docket;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .termsOfServiceUrl(termsOfServiceUrl)
                .contact(new Contact(contactName, contactUrl, contactEmail))
                .version(version)
                .build();
    }
}
